// 云函数入口文件
const cloud = require('wx-server-sdk')

var crypto = require('crypto');
cloud.init()
//生成腾讯云ocr授权签名
var secretId = 'AKID445vwMHANP7f2EMLjwyyqXQUO8h5BrXA',
  secretKey = 'c3KR5bzHPNAmDKG2e6NUREev42qTwEMB',
  appid = '1258450065',
  pexpired = 86400,
  userid = 0;

var now = parseInt(Date.now() / 1000),
  rdm = parseInt(Math.random() * Math.pow(2, 32)),
  plainText = 'a=' + appid + '&k=' + secretId + '&e=' + (now + pexpired) + '&t=' + now + '&r=' + rdm + userid + '&f=',
  data = new Buffer(plainText, 'utf8'),
  res = crypto.createHmac('sha1', secretKey).update(data).digest(),
  bin = Buffer.concat([res, data]);

//生成base64码
var sign = bin.toString('base64');

// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()

  return {
    sign,
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}